

#include <linux/version.h> 
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
#include <linux/config.h>
#endif
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/usb.h>
#include <asm/uaccess.h>
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,16)
#include <linux/usb/serial.h>
#else
#include "usb-serial.h"
#endif

#include "zte_ev.h"

#define  MAX_SETUP_DATA_SIZE   32 

static int debug=0;
void zte_ev_usb_serial_init(struct usb_serial_port *port)
{
   	struct usb_serial *serial = port->serial;
	int result = 0,len;
	unsigned char *buf;

       if (port->number != 0)  return;  

       printk("zte ev enter---\n"); 
	buf = kmalloc(MAX_SETUP_DATA_SIZE, GFP_KERNEL);


/*send 1st ctl cmd(CTL    21 22 01 00  00 00 00 00)*/
      len = 0;
      result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
				0x22, 0x21,
				0x0001, 0x0000, NULL, len,
				HZ * USB_CTRL_GET_TIMEOUT);
      dbg("result = %d\n",result);  

      /*send  2st cmd and recieve data*/
     /*
 16.0  CTL    a1 21 00 00  00 00 07 00                                                                                CLASS                   25.1.0(5)     
  16.0  DI     00 96 00 00  00 00 08    
      */
      len = 0x0007;
     result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
				0x21, 0xa1,
				0x0000, 0x0000, buf, len,
				HZ * USB_CTRL_GET_TIMEOUT);
     dbg("result = %d\n",result);
     if (result == len)	  
          usb_serial_debug_data(debug,&port->dev, __FUNCTION__, len,buf);

   /*send 3 cmd*/
   /*
  16.0 CTL    21 20 00 00  00 00 07 00                                                                                CLASS                   30.1.0        
  16.0  DO     80 25 00 00  00 00 08                                                                                   .%.....                 30.2.0        
  */
  len = 0x0007;
   buf[0]=0x80;
   buf[1]=0x25;
   buf[2]=0x00;
   buf[3]=0x00;
   buf[4]=0x00;
   buf[5]=0x00;
   buf[6]=0x08;
     result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
				0x20, 0x21,
				0x0000, 0x0000, buf, len,
				HZ * USB_CTRL_GET_TIMEOUT);
     dbg("result = %d\n",result);
     if (result == len)	  
          usb_serial_debug_data(debug,&port->dev, __FUNCTION__, len,buf);


   

   /*send 4 cmd*/
   /*
    16.0 CTL    21 22 03 00  00 00 00 00   
  */

      len = 0;
      result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
				0x22, 0x21,
				0x0003, 0x0000, NULL, len,
				HZ * USB_CTRL_GET_TIMEOUT);
      dbg("result = %d\n",result);  
   

   /*send 5 cmd*/
   /*
 16.0  CTL    a1 21 00 00  00 00 07 00                                                                                CLASS                   33.1.0        
  16.0  DI     80 25 00 00  00 00 08   
  */
   len = 0x0007;
     result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
				0x21, 0xa1,
				0x0000, 0x0000, buf, len,
				HZ * USB_CTRL_GET_TIMEOUT);
     dbg("result = %d\n",result);
     if (result == len)	  
          usb_serial_debug_data(debug,&port->dev, __FUNCTION__, len,buf);


  /*send 6 cmd*/
   /*
   16.0  CTL    21 20 00 00  00 00 07 00                                                                                CLASS                   34.1.0        
  16.0  DO     80 25 00 00  00 00 08 
  */
  len = 0x0007;
   buf[0]=0x80;
   buf[1]=0x25;
   buf[2]=0x00;
   buf[3]=0x00;
   buf[4]=0x00;
   buf[5]=0x00;
   buf[6]=0x08;
     result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
				0x20, 0x21,
				0x0000, 0x0000, buf, len,
				HZ * USB_CTRL_GET_TIMEOUT);
     dbg("result = %d\n",result);
     if (result == len)	  
          usb_serial_debug_data(debug,&port->dev, __FUNCTION__, len,buf);
	kfree(buf);
}


/*
      CTL    21 22 02 00  00 00 00 00                                                                                CLASS                  338.1.0         
  
 * 16.1  DI     a1 20 00 00  00 00 02 00  02 00                                                                         . ........             340.1.0        
  16.0  CTL    21 22 03 00  00 00 00 00                                                                                CLASS                  341.1.0        
 
  16.0  CTL    a1 21 00 00  00 00 07 00                                                                                CLASS                  346.1.0(3)     
  16.0  DI     00 08 07 00  00 00 08                                                                                   .......                346.2.0        

  16.0  CTL    21 20 00 00  00 00 07 00                                                                                CLASS                  349.1.0        
  16.0  DO     00 c2 01 00  00 00 08                                                                                   .......                349.2.0        

  16.0  CTL    21 22 03 00  00 00 00 00                                                                                CLASS                  350.1.0(2)     

  16.0  CTL    a1 21 00 00  00 00 07 00                                                                                CLASS                  352.1.0        
  16.0  DI     00 c2 01 00  00 00 08                                                                                   .......                352.2.0        

  *16.1  DI     a1 20 00 00  00 00 02 00  02 00                                                                         . ........             353.1.0        

  16.0  CTL    21 20 00 00  00 00 07 00                                                                                CLASS                  354.1.0        
  16.0  DO     00 c2 01 00  00 00 08                                                                                   .......                354.2.0        

  16.0  CTL    21 22 03 00  00 00 00 00    
*/

void zte_ev_usb_serial_exit(struct usb_serial_port *port)
{
   	struct usb_serial *serial = port->serial;
	int result = 0,len,actual_len;
	unsigned char *buf;

       if (port->number != 0)  return;

	buf = kmalloc(MAX_SETUP_DATA_SIZE, GFP_KERNEL);


      printk("zte ev exit---\n");
   /*send 1st ctl cmd(CTL    21 22 02 00  00 00 00 00)*/
      len = 0;
      result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
				0x22, 0x21,
				0x0002, 0x0000, NULL, len,
				HZ * USB_CTRL_GET_TIMEOUT);
      dbg("result = %d\n",result);  

 /*send 2st ctl cmd(CTL    21 22 03 00  00 00 00 00 )*/
      len = 0;
      result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
				0x22, 0x21,
				0x0003, 0x0000, NULL, len,
				HZ * USB_CTRL_GET_TIMEOUT);
      dbg("result = %d\n",result);  


      /*send  3st cmd and recieve data*/
     /*
 16.0  CTL    a1 21 00 00  00 00 07 00                                                                                CLASS                   25.1.0(5)     
  16.0  DI     00 08 07 00  00 00 08   
      */
      len = 0x0007;
     result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
				0x21, 0xa1,
				0x0000, 0x0000, buf, len,
				HZ * USB_CTRL_GET_TIMEOUT);
     dbg("result = %d\n",result);
     if (result == len)	  
          usb_serial_debug_data(debug,&port->dev, __FUNCTION__, len,buf);

   /*send 4 cmd*/
   /*
  16.0 CTL    21 20 00 00  00 00 07 00                                                                                CLASS                   30.1.0        
  16.0  DO     00 c2 01 00  00 00 08                                                                                 .%.....                 30.2.0        
  */
  len = 0x0007;
   buf[0]=0x00;
   buf[1]=0xc2;
   buf[2]=0x01;
   buf[3]=0x00;
   buf[4]=0x00;
   buf[5]=0x00;
   buf[6]=0x08;
     result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
				0x20, 0x21,
				0x0000, 0x0000, buf, len,
				HZ * USB_CTRL_GET_TIMEOUT);
     dbg("result = %d\n",result);
     if (result == len)	  
          usb_serial_debug_data(debug,&port->dev, __FUNCTION__, len,buf);


   

   /*send 5 cmd*/
   /*
    16.0 CTL    21 22 03 00  00 00 00 00    
  */

      len = 0;
      result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
				0x22, 0x21,
				0x0003, 0x0000, NULL, len,
				HZ * USB_CTRL_GET_TIMEOUT);
      dbg("result = %d\n",result);  
   

   /*send 6 cmd*/
   /*
 16.0  CTL     a1 21 00 00  00 00 07 00                                                                               CLASS                   33.1.0        
  16.0  DI     00 c2 01 00  00 00 08  
  */
   len = 0x0007;
     result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
				0x21, 0xa1,
				0x0000, 0x0000, buf, len,
				HZ * USB_CTRL_GET_TIMEOUT);
     dbg("result = %d\n",result);
     if (result == len)	  
          usb_serial_debug_data(debug,&port->dev, __FUNCTION__, len,buf);


  /*send 7 cmd*/
   /*
   16.0  CTL    21 20 00 00  00 00 07 00                                                                                CLASS                  354.1.0        
  16.0  DO     00 c2 01 00  00 00 08                                                                                   .......                354.2.0        
  */
  len = 0x0007;
   buf[0]=0x00;
   buf[1]=0xc2;
   buf[2]=0x01;
   buf[3]=0x00;
   buf[4]=0x00;
   buf[5]=0x00;
   buf[6]=0x08;
     result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
				0x20, 0x21,
				0x0000, 0x0000, buf, len,
				HZ * USB_CTRL_GET_TIMEOUT);
     dbg("result = %d\n",result);
     if (result == len)	  
          usb_serial_debug_data(debug,&port->dev, __FUNCTION__, len,buf);

  /*send 8 cmd*/
   /*
    16.0 CTL    21 22 03 00  00 00 00 00    
  */

      len = 0;
      result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
				0x22, 0x21,
				0x0003, 0x0000, NULL, len,
				HZ * USB_CTRL_GET_TIMEOUT);
      dbg("result = %d\n",result);  

	kfree(buf);
}

